/*
 * @lc app=leetcode.cn id=150 lang=cpp
 *
 * [150] 逆波兰表达式求值
 */

// @lc code=start
class Solution
{
public:
    int evalRPN(vector<string> &tokens)
    {
        //逆波兰表达式求值 先用一个栈保存 遇到数字直接存入 遇到运算符弹出字符计算后存入
        //只有加减乘除
        stack<int> a;
        for (int i = 0; i < tokens.size(); i++)
        {
            if (tokens[i] == "+" ||
                tokens[i] == "-" ||
                tokens[i] == "*" ||
                tokens[i] == "/")
            {
                int num1 = a.top();
                a.pop();
                int num2 = a.top();
                a.pop();
                if (tokens[i] == "+")
                {
                    a.push(num2 + num1);
                }
                if (tokens[i] == "-")
                {
                    a.push(num2 - num1);
                }
                if (tokens[i] == "*")
                {
                    a.push(num2 * num1);
                }
                if (tokens[i] == "/")
                {
                    a.push(num2 / num1);
                }
            }
            else
            {
                a.push(stoi(tokens[i]));
            }
        }
        int res = a.top();
        return res;
    }
};
// @lc code=end
